package net.yunzhanyi.client.service.impl;

import net.yunzhanyi.client.constant.AccountConstant;
import net.yunzhanyi.client.service.ClientAccountService;
import net.yunzhanyi.common.core.utils.ObjectUtils;
import net.yunzhanyi.common.core.utils.StringUtils;
import net.yunzhanyi.common.web.util.encoder.PasswordEncoder;
import net.yunzhanyi.generic.domain.model.ClientAccount;
import net.yunzhanyi.generic.domain.model.ClientUser;
import net.yunzhanyi.generic.mapper.ClientAccountMapper;
import net.yunzhanyi.generic.mapper.ClientUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author bestct
 * @date 2022/10/2
 * @type 类
 */
@Service
public class ClientAccountServiceImpl implements ClientAccountService {

    @Autowired
    private ClientAccountMapper clientAccountMapper;


    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private ClientUserMapper clientUserMapper;

    @Override
    public void register(String phone, String password) {
        //加密
        String encode = passwordEncoder.encode(password);
        ClientAccount clientAccount = new ClientAccount();
        clientAccount.setPhone(phone);
        clientAccount.setPassword(encode);
        clientAccount.setCreatedTime(System.currentTimeMillis());
        clientAccountMapper.insertSelective(clientAccount);
        createDefaultUser(clientAccount.getAid());
    }

    @Override
    public ClientAccount findAccountByAid(long aid) {
        return clientAccountMapper.selectByAid(aid);
    }

    @Override
    public boolean checkEmailUnique(String email) {
        ClientAccount account = clientAccountMapper.selectByEmail(email);
        return ObjectUtils.isNull(account) || ObjectUtils.isNull(account.getAid());
    }

    @Override
    public void bindPhone(long aid, String phone) {
        ClientAccount account = new ClientAccount();
        account.setAid(aid);
        account.setModifiedTime(System.currentTimeMillis());
        account.setPhone(phone);
        clientAccountMapper.updateByPrimaryKeySelective(account);

    }


    @Override
    public void bindEmail(long aid, String email) {
        ClientAccount account = new ClientAccount();
        account.setAid(aid);
        account.setModifiedTime(System.currentTimeMillis());
        account.setEmail(email);
        clientAccountMapper.updateByPrimaryKeySelective(account);
    }
    @Override
    public void changePassword(long aid, String newPassword) {
        ClientAccount account = new ClientAccount();
        account.setAid(aid);
        account.setModifiedTime(System.currentTimeMillis());
        account.setPassword(passwordEncoder.encode(newPassword));
        clientAccountMapper.updateByPrimaryKeySelective(account);
    }

    private void createDefaultUser(Long aid) {
        ClientUser user = new ClientUser();
        user.setAid(aid);
        user.setNickName(AccountConstant.DEFAULT_USER + "_" + StringUtils.getUniqueStr());
        user.setBirthday(new Date());
        user.setSignature("这个人很懒，还没设置签名");
        user.setCreatedTime(System.currentTimeMillis());
        clientUserMapper.insertSelective(user);
    }

    @Override
    public boolean checkPhoneUnique(String phone) {
        ClientAccount account = clientAccountMapper.selectByPhone(phone);
        return ObjectUtils.isNull(account) || ObjectUtils.isNull(account.getAid());
    }

}
